package structral;


public class DecoratorTest {
	public static void main(String[] args) {
		Beverage tea=new tea("红茶");
		System.out.println(String.format("%s:%d",tea.name,tea.getPrice()));
		
		Decorator addSugar = new addSugar("加糖");
		addSugar.decoratorate(tea);
		System.out.println(String.format("%s:%d",addSugar.name,addSugar.getPrice()));
		
		
	}
}
//被装饰父类
abstract class Beverage{ 
	public String name;
	public Beverage(String name) {			
		super();
		this.name = name;
	}
	public abstract int getPrice();
}				
class tea extends Beverage{

	public tea(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int getPrice() {
		// TODO Auto-generated method stub
		return 10;
	}
	
}
class coffee extends Beverage{

	public coffee(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int getPrice() {
		// TODO Auto-generated method stub
		return 15;
	}
	
}
//装饰父类
abstract class Decorator extends Beverage{

	public Decorator(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}
	protected Beverage beverage;//关联被装饰父类
	public void decoratorate(Beverage b) {
		this.beverage=b;
	}
	
}

class addSugar extends Decorator{

	public addSugar(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int getPrice() {
		// TODO Auto-generated method stub
		return beverage.getPrice()+5;
	}
	
}
class addMilk extends Decorator{

	public addMilk(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	@Override
	public int getPrice() {
		// TODO Auto-generated method stub
		return beverage.getPrice()+8;
	}
	
}